﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaiwuZD
{
    /// <summary>
    /// 生活技能
    /// </summary>
    public class TwSkill : TwObject
    {
        public static Dictionary<int, string> TwSkillTypes { get; private set; } = new Dictionary<int, string> {
            { 0,"琴" },{ 1,"棋" },{ 2,"书" }  ,{ 3,"画" },{ 4,"术数" } ,{ 5,"品鉴" },{ 6,"锻造" }  ,{ 7,"制木" },{ 8,"医术" },
            { 9,"毒术" },{ 10,"织绵" }  ,{ 11,"巧匠" },{ 12,"道法" }  ,{ 13,"佛学" },{ 14,"厨艺" },{ 15,"杂学" }};


        private int _Proficiency;
        //private int _Degree;
        private int[] _BooksReaded = new int[10];


        public int TypeId { get { return ValueAt<int>(3); } }


        public int Level { get { return ValueAt<int>(2); } }



        /// <summary>
        /// 修习程度
        /// </summary>
        public int Proficiency
        {
            get { return _Proficiency; }
            set { _Proficiency = Math.Min(Math.Max(0, value), 100); }
        }

        /// <summary>
        /// 心法等级
        /// </summary>
        public int Degree
        {
            get { return BooksReaded?.Count(x=>x ==1)??0; }
            set { var dd = Math.Min(Math.Max(0, value), 10); ResetBookRead(dd); }
        }


        public int[] BooksReaded
        {
            get { return _BooksReaded; }
            set { if (value?.Length ==10) SetProperty(ref _BooksReaded, value); }
        }

        private void ResetBookRead(int pages)
        {
            List<int> list = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                if (_BooksReaded[i] == 1)
                    list.Add(i);
            }

            var oldr = list.Count;
            if (oldr == pages)
                return;

            Random rd = new Random();
            for (int i = 0; i < oldr-pages; i++)
            {
                var nnid = rd.Next(0, list.Count - 1);
                _BooksReaded[list[nnid]] = 0;
                list.RemoveAt(nnid);
            }
            list = _BooksReaded.Except(list).ToList();
            for (int i = 0; i < pages - oldr; i++)
            {
                var nnid = rd.Next(0, list.Count - 1);
                _BooksReaded[list[nnid]] = 1;
                list.RemoveAt(nnid);
            }

        }

        public override object Clone()
        {
            var rr = new TwSkill() { Name = this.Name, Id = this.Id,  Description = this.Description };
            return rr;
        }


        protected override string GetDefaultValue(string key)
        {
            if (GameAssets.TwSkills != null)
            {
                if (GameAssets.TwSkills.ContainsKey(Id))
                    if (GameAssets.TwSkills[Id].DataShop.ContainsKey(key))
                        return GameAssets.TwSkills[Id].DataShop[key];
            }

            return base.GetDefaultValue(key);
        }
    }
}
